<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="phone">
        <el-date-picker
          v-model="queryParams.startDate"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerStartOptions"
          @change="selectedStartDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="fax">
        <el-date-picker
          v-model="queryParams.endDate"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerEndOptions"
          @change="selectedEndDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userTaskList" height="80vh">
      <el-table-column label="责任人" align="center" prop="assignUserName" />
      <el-table-column label="项目名称" align="left" prop="projectName" />
      <el-table-column
        label="任务名称"
        align="left"
        prop="taskName"
        :show-overflow-tooltip="true"
        width="400"
      />
      <el-table-column label="开始时间" align="center" prop="startDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="process">
        <template slot-scope="scope">
          <span :class="getRowClass(scope.row)">{{ scope.row.progress }}%</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="openAssmtForm(scope.row)"
            v-hasPermi="['task:assmt:add']"
            >考核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-alarm-clock"
            @click="openDeferList(scope.row)"
            v-hasPermi="['pms:task:save']"
            >延时申请
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pms:task:save']"
            >修改进度
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-show="scope.row.process > 0"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pms:task:delete']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--    项目任务-->
    <el-dialog
      width="1200px"
      :title="'任务信息'"
      :visible.sync="taskProcessVisible"
      :key="1"
    >
      <task-process ref="taskProcess" :taskId="taskId"></task-process>
    </el-dialog>
    <defer-list ref="deferList" />
    <task-assmt ref="taskAssmt" />
  </div>
</template>

<script>
import { listResources } from "@/api/pms/project/resources";
import { listUserTask } from "@/api/pms/project/taskreport";
import taskProcess from "@/views/pms/components/taskProcess.vue";
import deferList from "@/views/pms/project/deferList";
import taskAssmt from "@/views/pms/project/taskAssmt";
import moment from "moment";
export default {
  name: "Customer",
  components: { taskProcess,deferList,taskAssmt },
  data() {
    return {
      taskProcessVisible: false,
      // 遮罩层
      loading: true,
      taskId: undefined,
      // 总条数
      total: 0,
      // 用户任务
      userTaskList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assignUserId: null,
        startDate: null,
        endDate: null,
        taskName: null,
      },
      pickerStartOptions: {
          // disabledDate(time) {
          //   return time.getTime() > Date.now();
          // },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }, {
            text: '一月前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', date);
            }
          }]
        },
        pickerEndOptions: {
          // disabledDate(time) {
          //   return time.getTime() > Date.now();
          // },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '明天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }, {
            text: '一月后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit('pick', date);
            }
          }]
        },
    };
  },
  created() {
    let query = this.$route.query;
    this.queryParams.startDate = query.startDate;
    this.queryParams.endDate = query.endDate;
    this.getList();
  },
  methods: {
    formatDate(date) {
      return moment(date).format('YYYY-MM-DD');
    },
    selectedStartDate(data) {
      this.queryParams.startDate = this.formatDate(data);
    },
    selectedEndDate(data) {
      this.queryParams.endDate = this.formatDate(data);
    },
    openAssmtForm(row) {
      this.$refs.taskAssmt.show(row.id);
    },
    openDeferList(row) {
      this.$refs.deferList.show(row.id, row.startDate);
    },
    handleUpdate(task) {
      this.taskId = task.id;
      this.$nextTick(() => {
      if (this.$refs.taskProcess) {
        this.$refs.taskProcess.initTask();
      }else{
        console.log("没有taskProcess", this.$refs); 
      }
    });
      this.taskProcessVisible = true;
    },
    /** 查询客户列表 */
    getList() {
      let query = this.$route.query;
      this.queryParams.assignUserId = query.userId;
      this.queryParams.type = query.type;
      this.loading = true;
      listUserTask(this.queryParams).then((response) => {
        this.userTaskList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getRowClass(row) {
      return {
        "bold-text": row.progress == 100,
        "black-text": row.progress == 100,
      };
    },
  },
};
</script>

<style lang="scss">
.dialog-task-info {
  .el-descriptions-item__container .el-descriptions-item__label,
  .el-descriptions-item__container .el-descriptions-item__content {
    align-items: center;
    font-size: 16px;
  }
}
.box-card {
  margin-bottom: 10px;
}
</style>
